***********************************************************
***Construct neighborhood-level covariates from baseline***
***********************************************************

******************
***Clean up DKs***
******************
foreach var of varlist _all {
	cap confirm numeric variable `var'
	if !_rc {
		quietly: replace `var'= .d if `var'==8888|`var'==888|`var'==88888 // don't know
		quietly: replace `var'= .w if `var'==9999|`var'==99999|`var'==999 // don't want to say
		quietly: replace `var' = .n if `var'==7777 //not relevant / other exception
	}
}

*Reverse variables 
global reverse_vars = "tax42 f14 f15 f16 pol6"

foreach var in $reverse_vars{
revrs `var'
drop `var'
rename rev`var' `var'
}


***********************
***Code up variables***
***********************

g owner = d7==1 | d7b==1
replace owner =1 if inlist(a7, 379, 521, 667) 
keep if owner==1 // Drop renter values so data are representative of the population of interest (property owners)

*House quality

gen roof =0 if d2==1
replace roof=1 if d2==7 & d2_b==3
replace roof=2 if d2==7 & d2_b==2
replace roof=3 if d2==7 & d2_b==1
replace roof =4 if d2==6|d2==2

gen floor= d1

global hh_quality = "roof floor"

*Household size (number of adults)
g hh_size = d9
replace hh_size=1 if d9==0

*Weekly expenditure (on phone credit)
g expenditure = d5
global expenditure_weekly = "expenditure"

*Prior collector visits
g past_collector_visit=tax5==1
replace past_collector_visit=0 if tax5b_8888==1

g visited_past= tax5
replace visited_past=0 if tax4==0
replace visited_past=0 if tax5b_1==0

*Prior tax payment
g past_payment = tax15==2|tax15==4

*Knows provincial tax ministry
g knows_dgrkoc = tax2==2|tax2==1

*Trust in government
gen trust_gov = f14
replace trust_gov=. if f14==.

gen trust_dgrkoc = f15
replace trust_dgrkoc=. if f15==.

global gov_trust = "trust_dgrkoc trust_gov"

*Responsibility for public goods provision
foreach var of varlist e78 e79 e80 e81 e83 e84{
gen `var'_provgov=`var'==2
replace `var'_provgov=. if `var'==.|`var'==.d|`var'==.w
}
egen provgov_imp = rowtotal(*_provgov), missing

global govimpprovide = "provgov_imp"

*Integrity of provincial government spending
gen gov_corrupt = gov1 
replace gov_corrupt=. if gov1==.

global gov_corrupt = "gov_corrupt"

*Performance of government
gen gov_perform = pol6
gen dgrkoc_perform = tax42

global gov_eval = "dgrkoc_perform gov_perform"

*Political participation
gen vote_2011 = pol3==1
replace vote_2011=. if pol3==.|pol3==.d|pol3==.w

gen party = pol4==1
replace party=. if pol4==.d|pol4==.w

gen protest = pol5==1
replace protest=. if pol5==.d|pol5==.w|pol5==.
g past_protest = protest

global political_participation = "vote_2011 party protest"

*Trust in researchers
global trust_researchers = "f16"

******************
***Make indices***
******************

global indices_to_make = "hh_quality expenditure_weekly gov_trust gov_eval trust_researchers govimpprovide gov_corrupt political_participation "

foreach index in $indices_to_make{
foreach var in $`index'{
sum `var'
replace `var' = (`var'-r(min))/(r(max)-r(min)) //normalize
}
egen `index'_n = rowtotal($`index'), missing
sum `index'_n
replace `index'_n = (`index'_n-r(min))/(r(max)-r(min)) 
}

global vars = "hh_size hh_quality expenditure_weekly knows_dgrkoc past_collector_visit visited_past past_payment political_participation protest past_protest gov_trust gov_eval trust_researchers govimpprovide gov_corrupt" 

************************************
***Collapse to neighborhood level***
************************************

collapse (mean) $vars , by(a7)

label var hh_size "Number adults in household (neighborhood average)"
label var hh_quality_n "House quality index (neighborhood average)"
label var expenditure_weekly_n "Weekly expenditure (neighborhood average)"
label var knows_dgrkoc "Knows provincial tax ministry (neighborhood average)"
label var past_collector_visit "Reports any prior collector visit (neighborhood average)"
label var past_payment "Reports ever paying property tax (neighborhood average)"
label var visited_past "Reports any prior collector visit (neighborhood average)"
label var political_participation_n "Poltiical participation index (neighborhood average)"
label var protest "Past engagement in political protests (neighborhood average)"
label var past_protest "Past engagement in political protests (neighborhood average)"
label var gov_trust_n "Trust in government (neighborhood average)"
label var gov_eval_n "Performance of government (neighborhood average)"
label var trust_researchers_n "Trust in resaerchers (neighborhood average)"
label var govimpprovide_n "Resp. for public goods provision (neighborhood average)"
label var gov_corrupt "Integrity of government spending (neighborhood average)"

*********************************************
***Define dummies for het effects analysis***
*********************************************

foreach var in visited_past past_protest {
sum `var', d
gen `var'_hi = `var'>`r(p50)'
}

